var pageLimit = 20;
var pageName ='search'
$(document).ready(function () {
    var params = getParams();
    if (params.keyword) {
        getSearchData(params.keyword,1);
        getAbout(params.keyword);
    }
});


function getSearchData(keyword, pageNum) {
    let params = {keyword: keyword}
    $.ajax({
        url: '/api/search/index',
        type: 'get',
        data: { page: pageNum, limit: pageLimit, keyword: keyword},
        async: false,
        dataType: 'json',
        success: function (r) {
            let htmlStr;
            if (r.code === 200) {
                if (r.data.total > 0) {
                    $app.pager('pagination', 'getSearchData', params, pageNum, r.data.total, pageLimit);
                    htmlStr = '';
                    for (var i in r.data.list) {
                        htmlStr += `<div style="flex: 0 0 auto;width: 20%;">
                        <div class="category-layout">
                            <div class="category-thumb">
                                <div class="images-hover image">
                                    <a href="/detail/${ r.data.list[i]['_id']}">
                                        <img src="https://cdn.mihetong.com/${ r.data.list[i]['_source']['title_pic']}"
                                             alt="${ r.data.list[i]['_source']['title']}"
                                             title="${ r.data.list[i]['_source']['title']}"
                                             class="img-responsive m-auto">
                                    </a>
                                </div>
                                <div class="caption text-center">
                                    <h4 class="cat-title">
                                        <a href="/detail/${ r.data.list[i]['_id']}">${ r.data.list[i]['_source']['title']}</a>
                                    </h4>
                                    <div>
                                        <a href="/detail/${ r.data.list[i]['_id']}"
                                           class="featured-category-btn">查看详情</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>`
                    }
                } else {
                    var emptyStr = '<center><img src="/static/empty.png" style="margin-top:50px;width:300px;margin-bottom: 50px;"></center>';
                    $("#contractData").html(emptyStr);
                    $('#pagination').hide()
                    return false
                }
                $("#contractData").html(htmlStr);
            }
        }
    });
}

function getAbout(keyword){
    $.ajax({
        url: '/api/detail/about',
        type: 'get',
        data: { limit: 12, keyword: keyword,rand:12},
        async: false,
        dataType: 'json',
        success: function (r) {
            if (r.code == 200) {
                var aboutStr = '';
                if (r.data.list.length > 0) {
                    // for (var i in r.data.list) {
                    //     aboutStr += `<button type="button" class="btn btn-outline-primary"
                    //         style="margin-bottom: 5px;margin-left:20px" onClick="toUrl('/detail/${ r.data.list[i]['_source']['id']}')">${ r.data.list[i]['_source']['title']}</button>`
                    // }

                    var htmlStr = '';
                        for (var i in r.data.list) {
                            htmlStr += `<div class="owl-item active" style="width: 190px;float:left; margin-right: 30px;">
                                    <div class="item">
                                        <div class="category-layout col-12">
                                            <div class="category-thumb">
                                                <div class="images-hover image">
                                                    <a href="/detail/${ r.data.list[i]['_source'].id}" target="_blank">
                                                        <img src="https://cdn.mihetong.com/${ r.data.list[i]['_source'].title_pic}"
                                                             style="width:190px"
                                                             alt="${ r.data.list[i]['_source'].title}"
                                                             title="${ r.data.list[i]['_source'].title}"
                                                             class="img-responsive m-auto">
                                                    </a>
                                                </div>
                                                <div class="caption text-center">
                                                    <center>
                                                        <h4 class="cat-title">
                                                            <a href="/detail/${ r.data.list[i]['_source'].id}" target="_blank">${ r.data.list[i]['_source'].title}</a>
                                                        </h4>
                                                        <div>
                                                            <a href="/detail/${ r.data.list[i]['_source'].id}" target="_blank" class="featured-category-btn">
                                                                查看详情</a>
                                                        </div>
                                                    </center>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>`;
                        }
                } else {
                    var htmlStr = '<center><img src="/static/empty.png" style="margin-top:50px;width:250px;margin-bottom: 50px;"></center>';
                }
            }
            $("#aboutList").html(htmlStr);
        }
    });
}
function toUrl(url){
     window.open(url);
}

function getParams() {
    var params = {};
    var query = location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        params[pair[0]] = decodeURIComponent(pair[1]);
    }
    return params;
}
